//
//  CompanyFilterView.m
//  Btpin
//
//  Created by Vic on 2019/12/15.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "CompanyFilterView.h"
#import "UICollectionViewLeftAlignedLayout.h"
#import "PSCompanyCell.h"

@interface CompanyFilterView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
{
    UICollectionView *cCollectionView;
    
    NSString *scale; // 规模
    NSString *finance; // 融资
    NSString *trade; // 行业
}
@property (nonatomic, strong) NSMutableArray *itemDataArray;
@end

@implementation CompanyFilterView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.itemDataArray = [NSMutableArray arrayWithCapacity:0];
        scale = @"";
        finance = @"";
        trade = @"";
        self.filterShowFlag = NO;
        [self loadLocalData];
        
        [self initUI];
    }
    return self;
}
- (void)initUI {
    self.backgroundColor = COLOR_OF_WHITE;
    UILabel *titleLbl = [UIToolsControl initLabelWithFrame:CGRectMake(0, HeightOfStatusBar, WIDTH, 44) text:@"公司筛选" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_18];
    titleLbl.textAlignment = NSTextAlignmentCenter;
    [self addSubview:titleLbl];
    [titleLbl borderColor:THEMECOLOR_OF_Line_Color borderWidth:lineH borderType:BorderSideTypeBottom];
    UIButton *leftBtn = [UIToolsControl initButtonWithFrame:CGRectMake(10, HeightOfStatusBar, 44, 44) imageName:@"back_b" target:self action:@selector(dismissView) title:nil titleColor:nil];
//    leftBtn.imageEdgeInsets = UIEdgeInsetsMake(11, 0, 12, 32);
    [self addSubview:leftBtn];
    
    UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init];
    layout.minimumInteritemSpacing = 15*Scale;
    layout.minimumLineSpacing = 10*Scale;
    layout.headerReferenceSize = CGSizeMake(WIDTH-30*Scale, 50*Scale);
    layout.footerReferenceSize = CGSizeMake(WIDTH-30*Scale, 10*Scale);
    
    cCollectionView = [UIToolsControl initCollectionViewWithFrame:CGRectMake(LeftSpace, HeightOfStatusBarAndNavigationBar, WIDTH-30*Scale, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height-70) target:self collectionViewLayout:layout collectionViewCellID:isCellID registerClass:[PSCompanyCell class]];
    cCollectionView.showsVerticalScrollIndicator = NO;
    [self addSubview:cCollectionView];
    cCollectionView.backgroundColor = COLOR_OF_WHITE;
    
    [cCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CollectionViewHeaderViewID];
    [cCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:CollectionViewFooterViewID];
    
    UIView *bgView = [UIToolsControl initViewWithFrame:CGRectMake(0, EndY(cCollectionView), WIDTH, 70) backgroundColor:COLOR_OF_WHITE];
    [self addSubview:bgView];
    [bgView borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeTop];
    
    UIButton *resetBtn = [UIToolsControl initButtonWithFrame:CGRectMake(LeftSpace, 15, ScalePS(120), 40) imageName:nil target:self action:@selector(resetBtnTap:) title:@"重置" titleColor:THEMECOLOR_OF_TEXT_666666];
    resetBtn.backgroundColor = COLOR_OF_CLEAR;
    resetBtn.titleLabel.font = Font(Font_17);
    resetBtn.layer.cornerRadius = 20;
    resetBtn.layer.masksToBounds = YES;
    resetBtn.layer.borderColor = THEMECOLOR_OF_Line_Color.CGColor;
    resetBtn.layer.borderWidth = lineH;
    [bgView addSubview:resetBtn];
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(EndX(resetBtn)+ScalePS(10), 15, WIDTH-ScalePS(160), 40) imageName:nil target:self action:@selector(okBtnTap:) title:@"确定" titleColor:COLOR_OF_WHITE];
    okBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    okBtn.titleLabel.font = Font(Font_17);
    okBtn.layer.cornerRadius = 20;
    okBtn.layer.masksToBounds = YES;
    [bgView addSubview:okBtn];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (self.itemDataArray && self.itemDataArray.count) {
        return self.itemDataArray.count;
    }
    return 0;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.itemDataArray && self.itemDataArray.count) {
        PublicCategoryElementModel *tempModel = self.itemDataArray[section];
        return tempModel.childArray.count;
    }
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PSCompanyCell *cell = (PSCompanyCell *)[collectionView dequeueReusableCellWithReuseIdentifier:isCellID forIndexPath:indexPath];
    if (self.itemDataArray && self.itemDataArray.count) {
        PublicCategoryElementModel *tempModel = self.itemDataArray[indexPath.section];
        [cell reloadData:tempModel.childArray[indexPath.row]];
    }
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.itemDataArray && self.itemDataArray.count) {
        PublicCategoryElementModel *tempModel = self.itemDataArray[indexPath.section];
        PublicCategoryElementModel *tempModelSub = tempModel.childArray[indexPath.row];
        return CGSizeMake(tempModelSub.itemWidth, 40*Scale);
    }
    return CGSizeMake(ceil((WIDTH-60*Scale)/3)-1, 40*Scale);
}
//通过设置SupplementaryViewOfKind 来设置头部或者底部的view，其中 ReuseIdentifier 的值必须和 注册是填写的一致，本例都为 “reusableView”
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CollectionViewHeaderViewID forIndexPath:indexPath];
        for (UIView *view in headerView.subviews) {
            [view removeFromSuperview];
        }
        
        NSString *titleString = @"";
        if (self.itemDataArray && self.itemDataArray.count) {
            PublicCategoryElementModel *tempModel = self.itemDataArray[indexPath.section];
            titleString = tempModel.name;
        }
        UILabel *label = [UIToolsControl initLabelWithFrame:headerView.bounds text:titleString textColor:THEMECOLOR_OF_TEXT_333333 font:Font_15];
        label.font = FontBold(Font_15);
        [headerView addSubview:label];
        return headerView;
    } else {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:CollectionViewFooterViewID forIndexPath:indexPath];
        footerView.backgroundColor = COLOR_OF_WHITE;
        return footerView;
    }
    return nil;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.itemDataArray && self.itemDataArray.count) {
        PublicCategoryElementModel *tempModel = self.itemDataArray[indexPath.section];
        for (PublicCategoryElementModel *tempModelSub in tempModel.childArray) {
            tempModelSub.isSelect = NO;
        }
        PublicCategoryElementModel *model = self.itemDataArray[indexPath.section];
        PublicCategoryElementModel *modelSub = model.childArray[indexPath.row];
        modelSub.isSelect = YES;
    }
    [collectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
}
- (void)resetBtnTap:(UIButton *)button {
    if (self.itemDataArray && self.itemDataArray.count) {
        for (NSInteger i = 0; i < self.itemDataArray.count; i++) {
            PublicCategoryElementModel *tempModel = self.itemDataArray[i];
            for (NSInteger j = 0; j < tempModel.childArray.count; j++) {
                PublicCategoryElementModel *tempModelSub = tempModel.childArray[j];
                if (j == 0) {
                    tempModelSub.isSelect = YES;
                } else {
                    tempModelSub.isSelect = NO;
                }
            }
        }
    }
    [cCollectionView reloadData];
}
- (void)okBtnTap:(UIButton *)button {
    if (self.itemDataArray && self.itemDataArray.count) {
        for (NSInteger i = 0; i < self.itemDataArray.count; i++) {
            PublicCategoryElementModel *model = self.itemDataArray[i];
            for (NSInteger j = 0; j < model.childArray.count; j++) {
                PublicCategoryElementModel *tempModelSub = model.childArray[j];
                if (tempModelSub.isSelect) {
                    if ([tempModelSub.titleName isEqualToString:@"公司规模"]) {
                        scale = tempModelSub.id;
                    } else if ([tempModelSub.titleName isEqualToString:@"融资情况"]) {
                        finance = tempModelSub.id;
                    } else if ([tempModelSub.titleName isEqualToString:@"公司行业"]) {
                        trade = tempModelSub.id;
                    }
                }
            }
        }
    }
    [self dismissView];
    
    [self performSelector:@selector(performClick) withObject:nil afterDelay:0.5];
}
- (void)performClick {
    self.filter(scale, finance, trade);
}

- (void)showView {
    self.filterShowFlag = YES;
    __weak __typeof(self)weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        CGRect frame = strongSelf.frame;
        frame.origin.x = 0;
        strongSelf.frame = frame;
    }];
}
- (void)dismissView {
    self.filterShowFlag = NO;
    __weak __typeof(self)weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        CGRect frame = strongSelf.frame;
        frame.origin.x = WIDTH;
        strongSelf.frame = frame;
    }];
}


- (void)loadLocalData {
    if ([DataToolsControl sharedManager].allCategoryInfoModel) {
        PublicCategoryElementModel *companyModel1 = [[PublicCategoryElementModel alloc] init];
        companyModel1.name = @"公司规模";
        companyModel1.id = @"1";
        companyModel1.childArray = [NSMutableArray arrayWithCapacity:0];
        PublicCategoryElementModel *mmodel111 = [[PublicCategoryElementModel alloc] init];
        mmodel111.id = @"0";
        mmodel111.name = @"不限";
        mmodel111.titleName = @"公司规模";
        mmodel111.isSelect = YES;
        mmodel111.itemWidth = MAX([UIToolsControl textWidth:mmodel111.name size:CGSizeMake(MAXFLOAT, 30*Scale) font:Font_13]+13*Scale, ceil((WIDTH-60*Scale)/3)-1);
        [companyModel1.childArray addObject:mmodel111];
        for (NSInteger i = 0; i < [DataToolsControl sharedManager].allCategoryInfoModel.scaleArray.count; i++) {
            PublicCategoryElementModel *mmodel1 = [[DataToolsControl sharedManager].allCategoryInfoModel.scaleArray[i] copy];
            mmodel1.titleName = @"公司规模";
            [companyModel1.childArray addObject:mmodel1];
        }
        [self.itemDataArray addObject:companyModel1];
        
        PublicCategoryElementModel *companyModel2 = [[PublicCategoryElementModel alloc] init];
        companyModel2.name = @"融资情况";
        companyModel2.id = @"2";
        companyModel2.childArray = [NSMutableArray arrayWithCapacity:0];
        PublicCategoryElementModel *mmodel222 = [[PublicCategoryElementModel alloc] init];
        mmodel222.id = @"0";
        mmodel222.name = @"不限";
        mmodel222.titleName = @"融资情况";
        mmodel222.isSelect = YES;
        mmodel222.itemWidth = MAX([UIToolsControl textWidth:mmodel222.name size:CGSizeMake(MAXFLOAT, 30*Scale) font:Font_13]+13*Scale, ceil((WIDTH-60*Scale)/3)-1);
        [companyModel2.childArray addObject:mmodel222];
        for (NSInteger i = 0; i < [DataToolsControl sharedManager].allCategoryInfoModel.financeArray.count; i++) {
            PublicCategoryElementModel *mmodel2 = [[DataToolsControl sharedManager].allCategoryInfoModel.financeArray[i] copy];
            mmodel2.titleName = @"融资情况";
            [companyModel2.childArray addObject:mmodel2];
        }
        [self.itemDataArray addObject:companyModel2];
        
        PublicCategoryElementModel *companyModel3 = [[PublicCategoryElementModel alloc] init];
        companyModel3.name = @"公司行业";
        companyModel3.id = @"3";
        companyModel3.childArray = [NSMutableArray arrayWithCapacity:0];
        PublicCategoryElementModel *mmodel333 = [[PublicCategoryElementModel alloc] init];
        mmodel333.id = @"0";
        mmodel333.name = @"不限";
        mmodel333.titleName = @"公司行业";
        mmodel333.isSelect = YES;
        mmodel333.itemWidth = MAX([UIToolsControl textWidth:mmodel333.name size:CGSizeMake(MAXFLOAT, 30*Scale) font:Font_13]+13*Scale, ceil((WIDTH-60*Scale)/3)-1);
        [companyModel3.childArray addObject:mmodel333];
        for (NSInteger i = 0; i < [DataToolsControl sharedManager].allCategoryInfoModel.tradeArray.count; i++) {
            PublicCategoryElementModel *mmodel3 = [[DataToolsControl sharedManager].allCategoryInfoModel.tradeArray[i] copy];
            mmodel3.titleName = @"公司行业";
            [companyModel3.childArray addObject:mmodel3];
        }
        [self.itemDataArray addObject:companyModel3];
    }
}

@end
